package com.yonyou.crm.bpub.initData.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.bpub.initData.entity.InitDatasBodyVO;
import com.yonyou.crm.bpub.initData.entity.InitDatasListItemVO;
import com.yonyou.crm.bpub.initData.entity.InitDatasVO;
import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.exception.CrmBusinessException;

@Service
public class InitDatasBizServiceImpl extends BaseBizServiceImpl<InitDatasVO>{

	@Autowired
	CustomerInitDatas customerInitDatas;
	
	@Autowired
	OppInitData oppInitData;
	
	@Autowired
	ContactInitDatas contactInitDatas;
	
	@Autowired
	LeadInitDatas leadInitDatas;
	
	@Autowired
	CumprmtInitDatas cumprmtInitDatas;
	
	public <T extends Serializable> InitDatasVO getInitData(Map<String, String> param){
		String module = param.get("module");
		String id = param.get("id");
		InitDatasVO initData = new InitDatasVO();
		BaseInitDatas service = null;
		T obj = null;
		long biztypeId = 0;
		InitDatasVO layoutList = null;
		switch (module) {
			case "accounts" :
				service = customerInitDatas;
				biztypeId = 11;
				break;
			case "opportunities" :
				service = oppInitData;
				break;
			case "contacts" :
				service = contactInitDatas;
				break;
			case "leads" :
				service = leadInitDatas;
				break;
			case "cumprmts":
				service = cumprmtInitDatas;
				break;
			default:
				// nothing
		}
		if (id.equals("0")) {
			if (biztypeId != 0) {
				initData = service.getLayout(biztypeId);
			}
			ArrayList<InitDatasListItemVO> header = service.getHeader(initData);
			initData.setHeaderlist(header);
			ArrayList<InitDatasBodyVO> body = service.getBody(initData);
			initData.setBodylist(body);
		} else {
			long idValue = Long.valueOf(id);
			obj = service.getDetail(idValue);
			if (obj == null) {
				throw new CrmBusinessException("对象不存在");
			}
			if (biztypeId != 0) {
				initData = service.getLayout(obj);
				if (initData == null) {
					initData = new InitDatasVO();
				}
			}
			ArrayList<InitDatasListItemVO> header = service.getHeader(obj, initData);
			initData.setHeaderlist(header);
			ArrayList<InitDatasBodyVO> body = service.getBody(obj, initData);
			initData.setBodylist(body);
		}
		ArrayList<InitDatasBodyVO> bodyDefault = service.getBodyDefault();
		initData.setBodydefaultdatalist(bodyDefault);
		
		return initData;
	}

}
